﻿using Nectar.DAL.Common;
using P2PWeb.Common;
using P2PWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace P2PWeb.Controllers
{
        [AccountAuthorize(AccountType.Admin, AccountType.LookThrough)]
    public class BorrowsManagerController : BaseController
    {
        static Repository<P2PModelContainer> _context = new Repository<P2PModelContainer>();
        //
        // GET: /BorrowsManager/

        public ActionResult Index(int pageIndex = 1)
        {
            Pager<Campaign> pager = new Pager<Campaign>();
            pager.CurrentPageIndex = pageIndex;


            QueryParameter isDelParameter = new QueryParameter("isDel", false);
            QueryParameter[] pageParameters = pager.GetQueryParameters();
            pager.Items = _context.GetListOf<Campaign>("o.IsDel=@isDel and o.Status=2 order by o.CreationTime desc  Skip @Skip Limit @Limit", pageParameters.Union(new[] { isDelParameter }).ToArray()
                , new List<string>(new[] { "Account.AccountAuthentication", "Account", "CampUpLoad" })).ToList();
            pager.TotialItemsCount = _context.GetListOf<Campaign>("o.IsDel=@isDel and o.Status=2 order by o.CreationTime desc", new[] { isDelParameter }).Count;
            if (Request.IsAjaxRequest())
            {
                return PartialView("BorrowsManagerAjax", pager);
            }
            return View(pager);
        }


        public ActionResult BorrowDetail(int borrowId)
        {
            Campaign campaign = _context.GetEntityByID<Campaign>(borrowId, new List<string>() { "Account", "Account.AccountInfo", "Account.AccountAuthentication", "Account.WorkInfo" });
            return View(campaign);

        }

        private void ChangingBorrowStateToLookThrough(Campaign campaign)
        {
            if (campaign.Status != (int)CampaignState.LookThrough)
            {
                throw new Exception("订单的当前状态不正确，无法切换为审核状态");
            }
            campaign.Status = (int)CampaignState.LookThrough;
        }

        private void ChangingBorrowStateToCancel(Campaign campaign)
        {
            campaign.Status = (int)CampaignState.Draft;
        }

        private void ChangingBorrowStateToFinancing(Campaign campaign)
        {
            if (campaign.Status != (int)CampaignState.LookThrough)
            {
                throw new Exception("订单的当前状态不正确，无法切换为投标中");
            }
            campaign.Status = (int)CampaignState.Financing;
        }


        [ValidateAntiForgeryToken]
        public ActionResult ChangeBorrowState(int ID, int state)
        {
            Account account=_context.GetEntityByID<Account>(CurrentUserInfo.ID);
            Campaign campaign = _context.GetEntityByID<Campaign>(ID);
            switch ((CampaignState)state)
            {
                case CampaignState.Financing:
                    ChangingBorrowStateToFinancing(campaign);
                    break;
                case CampaignState.LookThrough:
                    ChangingBorrowStateToLookThrough(campaign);
                    break;
                case CampaignState.Cancel:
                    ChangingBorrowStateToCancel(campaign);
                    break;
                default:
                    throw new Exception("操作非法");
            }
            campaign.UpdateTime = DateTime.Now;
            campaign.UpdateUser = account.Name;
            _context.Update(campaign);
            return RedirectToAction("index");
        }
    }
}
